package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {
    /**
     * 获取营业额
     * @param begin
     * @param end
     * @return
     */
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;


    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end){
        List<LocalDate> dateList=new ArrayList<>();
        List<Double> turnoverList=new ArrayList<>();

        while (!begin.equals(end)){
            dateList.add(begin);
            begin=begin.plusDays(1);
        }
        dateList.add(end);


        for(LocalDate date:dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map=new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            map.put("status", Orders.COMPLETED);

            Double sum=orderMapper.sumByMap(map);
            if(sum==null)
                sum=0.0;
            turnoverList.add(sum);
        }
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end){
        List<LocalDate> dateList=new ArrayList<>();
        List<Integer> totalUserList=new ArrayList<>();
        List<Integer> newUserList=new ArrayList<>();


        while (!begin.equals(end)){
            dateList.add(begin);
            begin=begin.plusDays(1);
        }
        dateList.add(end);

        for(LocalDate date:dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap();
            map.put("endTime",endTime);

            Integer total = userMapper.sumByMap(map);
            map.put("beginTime",beginTime);
            Integer sum=userMapper.sumByMap(map);

            if(sum==null)
                sum=0;
            if(total==null)
                total=0;
            totalUserList.add(total);
            newUserList.add(sum);
        }
    return UserReportVO.builder().dateList(StringUtils.join(dateList,",")).totalUserList(StringUtils.join(totalUserList,",")).newUserList(StringUtils.join(newUserList,",")).build();

    }
    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end){
        List<LocalDate> dateList=new ArrayList<>();
        List<Integer> validOrderCountList=new ArrayList<>();
        List<Integer> orderCountList=new ArrayList<>();

        while (!begin.equals(end)){
            dateList.add(begin);
            begin=begin.plusDays(1);
        }
        dateList.add(end);

        for(LocalDate date:dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap();
            map.put("endTime",endTime);
            map.put("beginTime",beginTime);
            Integer order = orderMapper.countByMap(map);
            map.put("status",Orders.COMPLETED);
            Integer valOrder = orderMapper.countByMap(map);

            if(order==null)
                order=0;
            if(valOrder==null)
                valOrder=0;
            orderCountList.add(order);
            validOrderCountList.add(valOrder);
        }
        Integer totalOrderCount=0;
        Integer validOrderCount=0;

        for (Integer order : orderCountList) {
            totalOrderCount += order;
        }

        for(Integer valorder:validOrderCountList){
            validOrderCount+=valorder;
        }



        Double orderCompletionRate =0.0;
        if(totalOrderCount!=0)
            orderCompletionRate  = (double)validOrderCount/ (double)totalOrderCount;

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .validOrderCount(validOrderCount)
                .totalOrderCount(totalOrderCount)
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .orderCompletionRate(orderCompletionRate)
                .build();
    }
    /**
     * 前10销量统计
     * @param begin
     * @param end
     * @return
     */
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end){

        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        Map map=new HashMap();
        map.put("endTime",endTime);
        map.put("beginTime",beginTime);
        List<GoodsSalesDTO> goodsSalesDTOList = orderMapper.getTop10(beginTime,endTime);

        List<String > name =new ArrayList<>();
        List<Integer> number =new ArrayList<>();

         for(GoodsSalesDTO goodsSalesDTO:goodsSalesDTOList){
            name.add(goodsSalesDTO.getName());
            number.add(goodsSalesDTO.getNumber());
        }
         return SalesTop10ReportVO.builder()
                 .nameList(StringUtils.join(name,","))
                 .numberList(StringUtils.join(number,","))
                 .build();
    }
}
